package me
import grails.converters.JSON
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
import groovy.sql.Sql
import groovy.json.*
import groovy.json.JsonSlurper

class TablethemeController {

	def dataSource

	def store() {
  		def sql = new Sql(dataSource)
   		def results = [:]	  
    	def rows = sql.rows("select * from table_theme")
    	results.total = rows.size()
    	results = [success:true,total:rows.size(),results:rows]
    	render results as JSON
  	}

	
  	def delete() {
  		def tableTheme = TableTheme.findById(params.id)
  		tableTheme.delete(flush: true) 
  		def results = [success:true]
    	render results as JSON
  	}

    def save() {
		def sql = new Sql(dataSource)
   		def resuls = [code:0,message:'success']
   		def themeId = params.tableThemeId
      	def themeName = params.tableThemeName
      	def themeSource = params.tableThemeSource
      	if (themeId==null || themeId=="") {
  			def tableTheme = new TableTheme(themeName: themeName, themeSource: themeSource)
			tableTheme.save(failOnError: true)
   		} else {
   			  def tableTheme = TableTheme.findById(themeId)
          try {
              tableTheme.themeName = themeName
              tableTheme.themeSource = themeSource
              tableTheme.save(failOnError:true,flush:true) 
          } catch (e) {
              println e
          }

   		}
    	
    	render resuls as JSON
    }

    
   	
}
